package net.wangds.auth.role.ctrl;

import net.wangds.auth.grant.entity.GeneralGrantEntity;
import net.wangds.auth.grant.service.GrantService;
import net.wangds.auth.module.Grant;
import net.wangds.auth.module.ResourceSchemaEnum;
import net.wangds.auth.resource.comp.GeneralResourceComp;
import net.wangds.auth.role.comp.GeneralRoleComp;
import net.wangds.auth.role.entity.GeneralRoleEntity;
import net.wangds.jpa.Sorts;
import net.wangds.jpa.comp.AbstractRemovedComp;
import net.wangds.jpa.ctrl.AbstractJpaCompCtrl;
import net.wangds.web.entrypoint.BaseMultipleResp;
import net.wangds.web.entrypoint.BaseResp;
import net.wangds.web.entrypoint.PagingForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/auth/role")
public class RoleCtrl extends AbstractJpaCompCtrl<GeneralRoleEntity> {

    @SuppressWarnings("unused")
    @Autowired
    private GeneralRoleComp mmCmpRole;

    @SuppressWarnings("unused")
    @Autowired
    private GrantService mmServGrant;

    @SuppressWarnings("unused")
    @Autowired
    private GeneralResourceComp mmCmpRes;

    @Override
    protected AbstractRemovedComp<GeneralRoleEntity> fetchJpaComp() {
        return mmCmpRole;
    }

    @Override
    protected BaseMultipleResp<GeneralRoleEntity> listWithKeywords(PagingForm page) {
        return mmConvPage2Resp.convert(mmCmpRole.findByKeywords(page.getKeywords(), mmConvPf2Pr.convert(page)));
    }


    @SuppressWarnings("unused")
    @RequestMapping(value="/grantRes/{roleId}/{resId}/{privilege}", method = RequestMethod.POST)
    public BaseResp<Boolean> grantRes2Role(
            @PathVariable("roleId") String roleId,
            @PathVariable("resId") String resId,
            @PathVariable("privilege") int privilege){

        mmCmpRole.findById(roleId).ifPresent(job->
                mmCmpRes.findById(resId).ifPresent(res-> mmServGrant.grant(res, job, privilege)));

        return BaseResp.of(true);
    }

    @SuppressWarnings("unused")
    @RequestMapping(value="/ungrantRes/{roleId}/{resId}/{privilege}", method = RequestMethod.POST)
    public BaseResp<Boolean> ungrantRes2Role(
            @PathVariable("roleId") String roleId,
            @PathVariable("resId") String resId,
            @PathVariable("privilege") int privilege
    ){

        mmCmpRole.findById(roleId).ifPresent(job->
                mmCmpRes.findById(resId).ifPresent(res-> mmServGrant.ungrant(res, job, privilege)));

        return BaseResp.of(true);
    }


    @SuppressWarnings("unused")
    @RequestMapping(value="/all", method = RequestMethod.POST)
    public BaseMultipleResp<GeneralRoleEntity> all(){
        return BaseMultipleResp.list(mmCmpRole.findAll(Sorts.createTm_asc));
    }


    @SuppressWarnings("unused")
    @Transactional
    @RequestMapping(value = "/{roleId}/menuIds", method = RequestMethod.POST)
    public BaseResp<? extends Collection<String>> loadJobMenuIds(
            @PathVariable("roleId") String roleId) {
        BaseResp<List<String>> res = BaseResp.of(null);
        mmCmpRole.findById(roleId).ifPresent(role ->
                res.body(mmServGrant.listByOwnerIdAndSchemaAndAction(role.getId(), ResourceSchemaEnum.MENU, 15)
                        .stream().map(GeneralGrantEntity::getResId).collect(Collectors.toList())));
        return res;
    }

    @SuppressWarnings("unused")
    @Transactional
    @RequestMapping(value = "/{roleId}/grants", method = RequestMethod.POST)
    public BaseResp<List<? extends Grant>> loadJobGrants(
            @PathVariable("roleId") String roleId) {

        BaseResp<List<? extends Grant>> res = BaseResp.of(null);
        mmCmpRole.findById(roleId).ifPresent(user -> res.body(mmServGrant.listGrantedByOwnerId(roleId)));

        return res;
    }
}
